
#include "../libpatches.h"
#include "shortout.h"
#include "../garbage.h"
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

static void module_shortout_processor(module m);
void module_shortout_customizer (module m, char * name, char * value);

void module_shortout_initializer(module m) {
	m->customizer = module_shortout_customizer;
	m->processor = module_shortout_processor;
}

void module_shortout_customizer (module m, char * name, char * value) {
	customizer_count_inputs(m, name, value);
	if (m->input_count)
		m->storage = (void *) new_garbage(m->input_count, sizeof(short));
}

static void module_shortout_processor(module m) {
	input * i = m->inputs;
	int count = m->input_count;
	short * s = (short *) m->storage;

	/*
	fprintf(stderr, "processing shortouter\n");
	*/
	for (;count--; i++, s++)
		*s =
			***i > 1 
			? SHRT_MAX 
			: ***i < -1 
			? SHRT_MIN
			: ***i * SHRT_MAX
		;

	/*
	fprintf(stderr, "shortout: %p %d\n", m->storage, m->input_count);
	*/
	if (m->storage)
		fwrite(m->storage, sizeof(short), m->input_count, stdout);
}
